<?php
/**
 * when the params eq. &not_as_menu will hiddened in the menu.
 * 当params 等于&not_as_menu 时, 将不在菜单里显示. 只作为权限用.
 */
class Menu {
	var $path = array ();

	function editForm($menus, $curMenu = array ()) {
		$str = '<ul>';
		foreach ($menus as $key => $menuItem) {
			$this->path[] = $key;
			$temMenu = $curMenu[$key];
			$mkey = implode('][', $this->path);
			if ($temMenu['name'] == $menuItem['name']) {
				$checked = ' checked="true"';
			} else {
				$checked = '';

			}

			$mid = 'p_menu_' . implode('_', $this->path);
			$str .= '<li><input ' . $checked . ' type="checkbox" onclick="chkClick(this)" name="p_menu[' . $mkey . ']" id="' . $mid . '" value="[' . $mkey . ']"><label for="' . $mid . '">' . $menuItem['name'] . '</label>';
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$this->path[] = "items";
				$str .= $this->editForm($menuItem['items'], $temMenu['items']);
				array_pop($this->path);
			}
			$str .= '</li>';
			array_pop($this->path);

		}
		$str .= '</ul>';
		return $str;
	}

	function disMenu($menus, $class = "tree") {
		global $action, $model;
		if ($class != '') {
			$str = '<ul class="' . $class . '" id="adminMenu">';
		} else {
			$str = '<ul>';
		}
		$i = 1;
		
		//如果带有参数&not_as_menu 标示不显示
		foreach ($menus as $menuItem) {
			if(!empty($menuItem['params']) && ($menuItem['params'] == '&not_as_menu')) {
				continue;
			}
			
			$str .= '<li';
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				if (count($menus) == $i) {
					$str .= ' class="Root last" ';
				} elseif(isset ($menuItem['items']) && is_array($menuItem['items']) && $this->isSon($menuItem['items'],$model)) {
					$str .= ' class="Root" ';
				} else {
					$str .= ' class="Root closed" ';
				}

				$str .= '>';
				$str .= '<a><strong>';
				$str .= $menuItem['name'];
				$str .= '</strong></a>';
				$str .= $this->disMenu($menuItem['items'], '');
			} else {
				//分析地址类型
				$ma = explode("&action=",$menuItem['action']);
				
				if($model == $ma['0'] && $action==$ma['1']) {
						$tmStyle = ' style="color: #963; border: 1px solid #ccc; background: #eee no-repeat;padding-right: 3px;"';
				} else {
					$tmStyle = '';
				}
				$str .= '><a href="admin.php?model=' . $menuItem['action'] . $menuItem['params'] . '"'.$tmStyle.'>' . $menuItem['name'] . '</a>';
			}
			$str .= '</li>';
			$i++;

		}
		$str .= '</ul>';
		return $str;
	}

	function isSon ($items, $model) {
		if(!is_array($items)) {
			return false;
		}
		foreach($items as $tmItem) {
			$ma = explode("&action=",$tmItem['action']);
			if($ma['0'] == $model) {
				return true;
			}
		}
		return false;
	}

	function deMenu($menus, $all) {

		$newMenus = array ();
		foreach ($menus as $key => $menuItem) {
			$_menus = $all[$key];
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$newMenus[$key]['name'] = $_menus['name'];
				$newMenus[$key]['items'] = $this->deMenu($menuItem['items'], $_menus['items']);
			} else {
				$newMenus[$key] = $_menus;
			}

		}
		return $newMenus;

	}

	function check($model, $action, $menus) {
		if (($model == 'admin') && (($action == 'index') || ($action=='login'))) {
			return true;
		}

		foreach ((array) $menus as $key => $menuItem) {
			if (isset ($menuItem['items']) && is_array($menuItem['items']) && count($menuItem['items'])) {
				$res = $this->check($model, $action, $menuItem['items']);
				if ($res) {
					return true;
				}
			}
			elseif (!empty ($menuItem['model']) && !empty ($menuItem['action']) && ($menuItem['model'] == $model) && ($menuItem['action'] == $action)) {
				return true;
			}

		}
		return false;
	}

	/*function getMenu($user) {
		$path = $this->getMenuPath($user);
		if (is_file($path)) {
			return include $path;
		} else {
			return array ();
		}
	}*/
	function getMenu($group_id) {
		if(!($group_id > 0)) return array();
		global $db , $_menus;
		$group = $db->rows('SELECT * FROM admin_user_group WHERE group_id='.$group_id);
		if($group['permissions']) {
			if(substr($group['permissions'], -1) == ',') {
				$group['permissions'] = substr($group['permissions'], 0, -1);
			}
			$perArr = explode("," , $group['permissions']);
		} else {
			return array();
		}
		$userPermission = array();
		if(count($perArr) > 0)
		{
			foreach($perArr as $v)
			{
				if($v)
				{
					$tmArr = explode("]" , substr($v,0,-1));
					if(count($tmArr) == 4)
					{
						$key0 = substr($tmArr[0] , 1);
						$key1 = substr($tmArr[1] , 1);
						$key2 = substr($tmArr[2] , 1);
						$key3 = substr($tmArr[3] , 1);
						$userPermission[$key0][$key1][$key2][$key3] = $_menus[$key0][$key1][$key2][$key3];
					}
					if(count($tmArr) == 3)
					{
						$key0 = substr($tmArr[0] , 1);
						$key1 = substr($tmArr[1] , 1);
						$key2 = substr($tmArr[2] , 1);
						$userPermission[$key0][$key1][$key2] = $_menus[$key0][$key1][$key2];
					}
					if(count($tmArr) == 2)
					{
						$key0 = substr($tmArr[0] , 1);
						$key1 = substr($tmArr[1] , 1);
						$userPermission[$key0][$key1] = $_menus[$key0][$key1];
					}
					if($key1 == 'items') {
						$userPermission[$key0]['name'] = $_menus[$key0]['name'];
					} elseif($key2 == 'items') {
						$userPermission[$key0][$key1]['name'] = $_menus[$key0][$key1]['name'];
					} elseif($key3 == 'items') {
						$userPermission[$key0][$key1][$key2]['name'] = $_menus[$key0][$key1][$key2]['name'];
					}
				}
			}
		}
		return $userPermission;
	}

	function getMenuPath($user) {
		return APP_PATH . '/users_menus/user_' . $user . '_menu.php';
	}

	function removeMenu($user) {
		$path = $this->getMenuPath($user);
		if (is_file($path)) {
			unlink($path);
			return true;
		} else {
			return false;
		}

	}

}
?>